(function ($a) {
    'use strict';
    var app = $a.adminApp;
    app
            .controller(
                    'bannersController.Index',
                    function ($scope, RestFactory, $location, $localStorage, $timeout, notificationFactory, cocLoading) {
                        $scope.list = [];
                        $scope.orderByColumn = 'ACTIVATE';
                        $scope.reverse = true;
                        var ctr = {
                            api: '/api/banners',
                            load: function () {
                                cocLoading.trigger();
                                var self = this;
                                RestFactory.get(self.api).success(function (data) {
                                    $scope.list = data;
                                    $scope.currentPage = self.currentPage(); //current page
                                    $scope.entryLimit = 10; //max no of items to display in a page
                                    $scope.filteredItems = $scope.list.length; //Initially for no filter 
                                    $scope.totalItems = $scope.list.length;
                                    $timeout(function () {
                                        cocLoading.trigger();
                                    }, 10);
                                }).error(function (data) {
                                    $timeout(function () {
                                        cocLoading.trigger();
                                    }, 10);
                                });
                            },
                            delete: function (id) {
                                RestFactory.delete(this.api + '/' + id).success(function (data) {
                                    notificationFactory.success(data.message);
                                    ctr.load();
                                }).error(function (data) {
                                    notificationFactory.error(data.message);
                                });
                            },
                            currentPage: function () {

                                var hasPage = $localStorage.get(this.$controller);
                                if (!hasPage) {
                                    $localStorage.set(this.$controller, 1);
                                    hasPage = 1;
                                }
                                return hasPage;
                            },
                            $controller: 'page_' + $location.$$absUrl,
                            $currentPage: function (pageNo) {
                                $localStorage.set(this.$controller, pageNo);
                            }
                        };

                        ctr.load();
                        $scope.setPage = function (pageNo) {
                            $scope.currentPage = pageNo;
                            ctr.$currentPage(pageNo);
                        };
                        $scope.filter = function () {
                            $timeout(function () {
                                $scope.filteredItems = $scope.filtered.length;
                            }, 10);
                        };
                        $scope.resetSearch = function () {
                            $scope.filteredItems = $scope.list.length;
                            $scope.search = '';
                        };

                        $scope.orderByFnc = function (item, forceString) {
                            var forceStr = forceString || false;
                            if (isNaN(item[$scope.orderByColumn]) || forceString) {
                                return item[$scope.orderByColumn];
                            }
                            return parseInt(item[$scope.orderByColumn], 10);
                        };
                        $scope.sort_by = function (predicate) {
                            //$scope.predicate = predicate;
                            $scope.orderByColumn = predicate;
                            $scope.reverse = !$scope.reverse;
                        };


                        $scope.delete = function (id) {
                            notificationFactory.confirm({
                                message: 'Deseja apagar esse registro ?',
                                callbackConfirm: function () {
                                    ctr.delete(id);
                                }
                            });
                        };


                    });

    app.controller('bannersController.Settings', function ($scope, $location,
            $timeout, $routeParams, cocGridDataFactory, notificationFactory, cocLoading) {

        var $table = 'banners', $_id = $routeParams.id || 0;



        $scope.files = [];
        cocLoading.trigger();
        $scope.uploadedBanner = function ($data, $status) {
        };
        $scope.triggerButton = function () {
            jQuery('input[onbuttonupload]').trigger('click');
        };
        $scope.showError = function (files, type, msg) {

        };
        $scope.sellerProgress = function () {

        };
        $scope.onComplete = function (files, data, obj) {

            if (data.files.length == 0) {
                notificationFactory.error(data.message);

                return;
            }
            $scope.files.push(data.files);
            $scope.banner.IMAGE = data.files.name;

        };

        $scope.onDeleteImageBanner = function (file) {
            cocLoading.trigger();
            cocGridDataFactory('on_delete_banner').save({
                'ID': file
            }, function () {
                $scope.files = [];
                cocLoading.trigger();
            });
        };

        shortcut
                .add(
                        "Ctrl+S",
                        function () {
                            if ($scope.BannerForm.$valid) {
                                $scope.save();
                            } else {
                                notificationFactory
                                        .error('Verifique os campos obrigatórios, esse formulário não pode ser submetido');
                            }
                        });


        $scope.save = function ($redirect) {
            var $redirect = $redirect || false;
            $scope.banner.IMAGE = $('input[name="IMAGE"]').val();
            cocLoading.trigger();
            if ($_id > 0) {
                cocGridDataFactory($table).update({
                    'id': $_id
                }, $scope.banner, function () {
                    notificationFactory.success();
                    cocLoading.trigger();
                    if ($redirect) {
                        $location.path($redirect);
                    }
                }, function (e) {

                    notificationFactory.error(e.data.message);
                    cocLoading.trigger();
                });
                return;
            } else {
                cocGridDataFactory($table).save($scope.banner, function () {
                    notificationFactory.success();
                    $scope.banner = {};
                    $scope.files = [];
                    cocLoading.trigger();

                    if ($redirect) {
                        $location.path($redirect);
                    }
                }, function (e) {
                    notificationFactory.error(e.data.message);
                    cocLoading.trigger();
                });
            }

        };

        if ($_id > 0) {
            $timeout(function () {
                cocGridDataFactory($table).query({
                    id: $_id
                }, function (data) {
                    $scope.banner = data['0'];
                    if ($scope.banner.IMAGE !== '') {
                        $scope.files.push({
                            'name': $scope.banner.IMAGE,
                            'loaded': false
                        });
                    }

                    cocLoading.trigger();
                }, function (e) {
                });

            }, 1000);

        } else {
            $timeout(function () {
                cocLoading.trigger();
            }, 500);
        }

    });

    app.controller('bannersController.Actvation', function ($scope,
            $routeParams, $location, $timeout, cocGridDataFactory,
            notificationFactory) {

        if ($routeParams.id && $routeParams.type) {
            cocGridDataFactory('update_status_banners').update({
                ID: $routeParams.id,
                ACTIVATE: $routeParams.type
            }, function (re) {

            });

        }

        $timeout(function () {
            notificationFactory.success();
            $location.path('/banners');
        }, 500);

    });
})(cocApp);